package com.funmi.lizip.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.Options;
import com.funmi.lizip.entity.Task;
import com.funmi.lizip.entity.dto.options.OptionsAddRequest;
import com.funmi.lizip.entity.dto.options.OptionsQueryRequest;
import com.funmi.lizip.entity.dto.options.OptionsUpdateRequest;
import com.funmi.lizip.entity.dto.task.TaskAddRequest;
import com.funmi.lizip.entity.dto.task.TaskUpdateRequest;
import com.funmi.lizip.entity.dto.taskDelivery.TaskDeliveryQueryRequest;
import com.funmi.lizip.entity.vo.OptionsVo;
import com.funmi.lizip.entity.vo.TaskVo;
import com.funmi.lizip.service.OptionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@Api(tags = "数据采集和核查")
@RequestMapping("options")
public class OptionsController {

    @Autowired
    private OptionsService  optionsService;

    @ApiOperation(value = "选项的新增")
    @PostMapping("")
    public R addOptions(@RequestBody OptionsAddRequest request){
        if(request==null){
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }

        request.setCreateAt(LocalDateTime.now());
        request.setIsDelete(false);
        Options options = new Options();
        BeanUtils.copyProperties(request,options);
        boolean save = optionsService.save(options);

        if (save) {
            return R.success().message("新增成功");
        } else {
            throw new ApplicationException("新增失败");
        }
    }


    @ApiOperation(value = "选项的修改")
    @PutMapping("/{id}")
    public R updateTask(@RequestBody(required = false) OptionsUpdateRequest request){
        if(request==null||request.getId()==null){
            throw new ApplicationException(ResultCode.NOT_FOUND);
        }
        Options options = new Options();
        BeanUtils.copyProperties(request,options);

        boolean update = optionsService.updateById(options);
        if (update) {
            return R.success().message("修改成功");
        } else {
            throw new ApplicationException("修改失败");
        }
    }

    @ApiOperation(value = "选项的删除")
    @DeleteMapping("/{id}")
    public R deleteTask(@PathVariable Integer id){
        if (id <= 0) {
            throw new ApplicationException(ResultCode.FILE_DELETE_ERROR);
        }
        boolean result = optionsService.removeById(id);
        if (result) {
            return R.success().message("删除成功");
        } else {
            return R.fail().message("删除失败");
        }
    }

    @ApiOperation("根据选项输入进行检索")
    @GetMapping("")
    public R listPage(OptionsQueryRequest request){
        long current = 1;
        long size = 5;
        Options optionsQuery = new Options();
        if(request!=null){
            BeanUtils.copyProperties(request,optionsQuery);
            current =request.getPage();
            size =request.getPageSize();
        }

        QueryWrapper<Options> qw = new QueryWrapper<>(optionsQuery);
        qw.select().orderByDesc("create_at");
        Page<Options> optionsPage = optionsService.page(new Page<>(current, size), qw);

        PageDTO<OptionsVo> optionsVoPageD= new PageDTO<>(optionsPage.getCurrent(), optionsPage.getSize(), optionsPage.getTotal());

        List<OptionsVo> optionsVoList = optionsPage.getRecords().stream().map(options -> {
            OptionsVo optionsVo = new OptionsVo();
            BeanUtils.copyProperties(options, optionsVo);
            return optionsVo;
        }).collect(Collectors.toList());
        optionsVoPageD.setRecords(optionsVoList);
        return R.success().data(optionsVoPageD);
    }
}
